/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.service.resource;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.cache.ResTypeDispConfCache;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.common.vo.ResTypeDispConf;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author fanshengchao
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-13 上午10:07:11          fanshengchao        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class ResTypeDispConfService {
    @Autowired
    private BaseDao baseDao;
    @Autowired
    private ResTypeDispConfCache resTypeDispConfCache;

    @Transactional(propagation = Propagation.REQUIRED)
    public void batchSaveOrUpdate(List<ResTypeDispConf> dispConfs) {

        for (int i = 0; i < dispConfs.size(); i++) {
            ResTypeDispConf dispConf = dispConfs.get(i);
            baseDao.saveOrUpdate(dispConf);
            resTypeDispConfCache.put(dispConf);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteResTypeDispConfByResId(Long resId) {
        List<ResTypeDispConf> dispConfs = resTypeDispConfCache.findByResId(resId);
        if (dispConfs != null) {
            for (int i = dispConfs.size() - 1; i >= 0; i--) {
                ResTypeDispConf dispConf = dispConfs.get(i);
                baseDao.delete(dispConf);
                resTypeDispConfCache.removeById(dispConf.getId());
            }
        }
    }

    /**
     * 先删除原有 在保存
     * @param resId
     * @param dispConfs
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void save(Long resId, List<ResTypeDispConf> dispConfs) {

        deleteResTypeDispConfByResId(resId);

        batchSaveOrUpdate(dispConfs);
    }

}
